// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <memory>
#include <vector>

#include <turbo/utility/status.h>
#include <nebula/types/type_fwd.h>
#include <nebula/util/type_fwd.h>
#include <turbo/base/macros.h>

namespace nebula::json {

    class PromotionGraph;

    class TURBO_EXPORT ChunkedArrayBuilder {
    public:
        virtual ~ChunkedArrayBuilder() = default;

        /// Spawn a task that will try to convert and insert the given JSON block
        virtual void insert(int64_t block_index,
                            const std::shared_ptr<Field> &unconverted_field,
                            const std::shared_ptr<Array> &unconverted) = 0;

        /// Return the final chunked array.
        /// Every chunk must be inserted before this is called!
        virtual turbo::Status finish(std::shared_ptr<ChunkedArray> *out) = 0;

        /// finish current task group and substitute a new one
        virtual turbo::Status ReplaceTaskGroup(
                const std::shared_ptr<nebula::internal::TaskGroup> &task_group) = 0;

    protected:
        explicit ChunkedArrayBuilder(
                const std::shared_ptr<nebula::internal::TaskGroup> &task_group)
                : task_group_(task_group) {}

        std::shared_ptr<nebula::internal::TaskGroup> task_group_;
    };

    /// create a chunked builder
    ///
    /// if unexpected fields and promotion need to be handled, promotion_graph must be
    /// non-null
    TURBO_EXPORT turbo::Status MakeChunkedArrayBuilder(
            const std::shared_ptr<nebula::internal::TaskGroup> &task_group, MemoryPool *pool,
            const PromotionGraph *promotion_graph, const std::shared_ptr<DataType> &type,
            std::shared_ptr<ChunkedArrayBuilder> *out);

}  // namespace nebula::json
